package com.chat.api.web.restctrl;

import cn.hutool.http.HttpUtil;
import com.chat.api.baidu.TransApi;
import com.chat.api.utils.DecideLangueUtil;
import com.chat.api.utils.ResultUtil;
import com.chat.api.web.vo.MessageEntity;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

@RestController
@RequestMapping("/misc/")
public class MiscCtrl {

    @Value("${baidu.appId}")
    private String APP_ID;
    @Value("${baidu.securityKey}")
    private String SECURITY_KEY;
    @Value("${wangyiyun.randomMusic}")
    private String RANDOM_MUSIC_URL;

    @RequestMapping(value = "translate",method = RequestMethod.POST)
    @ApiOperation("翻译")
    public MessageEntity translate(HttpServletRequest request,
                                   @RequestParam(value = "msg") String msg,
                                   @RequestParam(value = "from") String from,
                                   @RequestParam(value = "type") String type) {
        TransApi api = new TransApi(APP_ID, SECURITY_KEY);
        boolean chinese = DecideLangueUtil.isChinese(msg);
        String transResult = api.getTransResult(msg, from, type);
        JSONObject jsonObject = JSONObject.fromObject(transResult);
        JSONArray result = jsonObject.getJSONArray("trans_result");
        JSONObject object = result.getJSONObject(0);
        String dst = object.getString("dst");
        return ResultUtil.success(dst);
    }

    @RequestMapping(value = "searchMusic",method = RequestMethod.POST)
    @ApiOperation("随机音乐")
    public MessageEntity searchMusic(HttpServletRequest request) throws IOException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("sort","热歌榜");
        paramMap.put("format","json");
        String s = HttpUtil.post(RANDOM_MUSIC_URL,paramMap);
        JSONObject data1 = JSONObject.fromObject(s);
        JSONArray playlists = data1.getJSONArray("playlists");
        for (Object playlist : playlists) {

        }

        return ResultUtil.success(paramMap);
    }

}
